package com.pmcc.app.web;
import com.pmcc.core.config.security.JwtTokenUtil;
import com.pmcc.core.service.JwtAuthenticationResponse;
import com.pmcc.utils.ResultBean;
import com.pmcc.yn.entity.OrderList;
import com.pmcc.yn.entity.StoreInfo;
import com.pmcc.yn.service.EntryInfoService;
import com.pmcc.yn.service.OrderListService;
import com.pmcc.yn.service.StoreInfoService;
import com.pmcc.yn.service.TechnicianInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mobile.device.Device;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author chuxy 2019/4/15
 */
@RestController()
@RequestMapping("/api/v1/store")
public class StoreController {


    @Value("${jwt.header}")
    private String tokenHeader;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private StoreInfoService storeInfoService;

    @Autowired
    private TechnicianInfoService technicianInfoService;

    @Autowired
    private OrderListService orderListService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private EntryInfoService entryInfoService;

    @ApiOperation(value="统一登录接口", notes="返回的jwtToken要放在请求头当中")
    @PostMapping(value = "${jwt.route.authentication.path}")
    @ResponseBody
    public JwtAuthenticationResponse createAuthenticationToken(String username,String password, Device device) throws AuthenticationException {
        JwtAuthenticationResponse jwtAuthenticationResponse = new JwtAuthenticationResponse("error");
        // 刷新当前用户的
        final UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        try{
            // 根据登录的用户名和密码封装成 authentication
            final Authentication authentication = authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(
                            username,
                            password
                    )
            );
            SecurityContextHolder.getContext().setAuthentication(authentication);

            final String token = jwtTokenUtil.generateToken(userDetails, device);
            String auth="";
            if(userDetails.getAuthorities().size()>0){
                auth=userDetails.getAuthorities().toArray()[0].toString();
            }

            // Return the token
            return new JwtAuthenticationResponse(token,"ok",auth,"account");
        }catch (Exception e){
            jwtAuthenticationResponse.setCurrentAuthority("guest");
            jwtAuthenticationResponse.setType("account");
            return jwtAuthenticationResponse;
        }

    }
    /**
     * 门店报名关注状态
     * @param id 门店id
     * @return 返回报名人数变化、关注人数变化
     */
    @ApiOperation(value = "门店报名关注状态 ", httpMethod = "GET", notes = "门店id", response = ResultBean.class)
    @GetMapping(value = "storeStatus")
    public ResultBean storeStatus(String id){
        /**
         * 移动端传入门店id，返回报名人数变化、关注人数变化
         *
         */
        return entryInfoService.storeStatus(id);
    }
    /**
     * 门店报名关注状态详情
     * @param id 门店id
     * @return 返回变化操作、技师名称、、技师头像、图片信息、任务内容、技师综合评分、
     */
    @ApiOperation(value = "门店报名关注状态详情", httpMethod = "GET", notes = "门店id", response = ResultBean.class)
    @GetMapping(value = "statusShow")
    public ResultBean statusShow(String id,String type){
        /**
         * 移动端传入门店id，返回变化操作、技师名称、技师头像、图片信息、任务内容、技师综合评分
         * 取消订单附带理由、-20分
         *
         */
        return technicianInfoService.statusShowDetail(id,type);
    }
    /**
     * 门店接单状态
     * @param id 门店id
     * @return 返回订单列表
     */
    @ApiOperation(value = "门店订单状态列表 ", httpMethod = "GET", notes = "门店id", response = ResultBean.class)
    @GetMapping(value = "orderStatus")
    public ResultBean orderStatus(String id){
        /**
         * 移动端传入门店id，返回订单列表
         * 订单内容包含汽车图片、车型、服务类型、任务时间、赏金、报名人数、订单状态（已确认显示技师姓名）
         *
         */
        return storeInfoService.orderStatus(id);
    }
    /**
     * 报名技师列表
     * @param id 订单id
     * @return 返回报名技师列表
     */
    @ApiOperation(value = "报名技师列表 ", httpMethod = "GET", notes = "订单id", response = ResultBean.class)
    @GetMapping(value = "entryList")
    public ResultBean entryList(String id){
        /**
         * 移动端传入订单id，返回报名技师列表
         * 技师头像、姓名、接单次数、满意度、技师等级
         *
         */
        return technicianInfoService.entryList(id);
    }
    /**
     * 查看技师信息
     * @param id 技师id
     * @return 返回技师头像、姓名、接单次数、满意度、技师等级、综合评分、历史评价列表
     */
    @ApiOperation(value = "查看技师信息 ", httpMethod = "GET", notes = "技师id", response = ResultBean.class)
    @GetMapping(value = "teachInfo")
    public ResultBean teachInfo(String id){
        /**
         * 移动端传入技师id，返回技师头像、姓名、接单次数、满意度、技师等级、综合评分、历史评价列表
         * 评价包含门店名称、打分、服务类型、评价、评分照片、完成时间
         *
         */
        return technicianInfoService.teachInfo(id);
    }
    /**
     * 确认合作
     * @param id 技师id
     * @param orderId 订单id
     * @return
     */
    @ApiOperation(value = "确认合作", httpMethod = "GET", notes = "技师id,订单id", response = ResultBean.class)
    @GetMapping(value = "techAccept")
    public ResultBean techAccept(String id,String orderId){
        /**
         * 传入技师id，订单id，修改订单的状态，确认该订单由该技师来执行
         *
         *
         */
        return orderListService.techAccept(id,orderId);
    }
    /**
     * 追加赏金
     * @param id 订单id
     * @param money 追加金额
     * @return
     */
    @ApiOperation(value = "追加赏金", httpMethod = "POST", notes = "订单id,追加金额", response = ResultBean.class)
    @PostMapping(value = "updateMoneyById")
    public ResultBean updateMoneyById(String id,String money){
        /**
         * 传入订单id，追加金额，修改订单的金额信息
         *
         *
         */
        return orderListService.updateMoneyById(id,money);
    }
    /**
     * 发布订单
     * @param orderList 订单实体
     * @param file 上传图片
     * @return
     */
    @ApiOperation(value = "发布订单 ", httpMethod = "POST", notes = "订单实体,  上传图片", response = ResultBean.class)
    @PostMapping(value = "addOrder")
    public ResultBean addOrder(OrderList orderList,@RequestParam("file") MultipartFile file){
        /**
         * 移动端传入订单实体，
         *
         */
        return orderListService.addOrder(orderList,file);
    }
    /**
     * 门店订单待评价
     * @param id 门店id
     * @param flag 状态（待评价、已完成）
     * @return 返回符合的订单列表
     */
    @ApiOperation(value = "门店订单待评价 ", httpMethod = "GET", notes = "门店id,状态（待评价、已完成）", response = ResultBean.class)
    @GetMapping(value = "storeOrderByFlag")
    public ResultBean storeOrderByFlag(String id,String flag){
        /**
         * 移动端传入门店id、状态，返回符合的订单列表
         *订单包含任务时间、任务内容、赏金、采纳技师姓名
         *
         */
        return orderListService.storeOrderByFlag(id,flag);
    }
    /**
     * 去评价
     * @param id 订单id
     * @return 返回技师名字，头像，电话，技师综合评分
     */
    @ApiOperation(value = "去评价 ", httpMethod = "GET", notes = "订单id", response = ResultBean.class)
    @PostMapping(value = "storeAssess")
    public ResultBean storeAssess(String id){
        /**
         * 移动端传入订单id，得到技师名字，头像，电话，技师综合评分
         *
         */
        return technicianInfoService.storeAssess(id);
    }
    /**
     * 发表评价
     * @param id 订单id
     * @param Score  评分
     * @param Assess  评价
     * @param file  评分照片
     * @return 返回技师名字，头像，电话，技师综合评分
     */
    @ApiOperation(value = "发表评价 ", httpMethod = "POST", notes = "订单id，评分，评价，评分照片", response = ResultBean.class)
    @PostMapping(value = "addAssess")
    public ResultBean addAssess(String id,String Score,String Assess,@RequestParam("file") MultipartFile file){
        /**
         * 移动端传入订单id，评分，评价，评价照片
         *
         */
        return storeInfoService.addAssess(id,Score,Assess,file);
    }
    /**
     * 门店个人中心
     * @param id 门店id
     * @return 返回门店名称、门店照片、门店地址、电话、联系人、身份证正反面照片、订单记录（条数，显示最近一条）
     */
    @ApiOperation(value = "门店个人中心 ", httpMethod = "GET", notes = "门店id", response = ResultBean.class)
    @GetMapping(value = "storeCenter")
    public ResultBean storeCenter(String id){
        /**
         * 移动端传入门店id，返回门店名称、门店照片、头像、门店地址、电话、年龄、联系人、身份证正反面照片、订单记录（条数，显示最近一条）
         *订单内容有任务内容、任务时间、赏金、技师姓名
         *
         */
        return storeInfoService.storeCenter(id);
    }
    /**
     * 门店个人中心
     * @param id 门店id
     * @return 返回门店名称、头像、门店照片、电话、联系人、所在地、身份证正反面照片
     */
    @ApiOperation(value = "编辑门店信息 ", httpMethod = "GET", notes = "门店id", response = ResultBean.class)
    @GetMapping(value = "storeById")
    public ResultBean storeById(String id){
        /**
         * 移动端传入门店id，返回门店名称、头像、门店照片、电话、联系人、所在地、身份证正反面照片
         *
         */
        return storeInfoService.storeById(id);
    }
    /**
     * 修改保存个人信息
     * @param storeInfo 门店实体
     * @return
     */
    @ApiOperation(value = "修改保存个人信息 ", httpMethod = "POST", notes = "门店实体", response = ResultBean.class)
    @PostMapping(value = "updateStore")
    public ResultBean updateStore(StoreInfo storeInfo){
        /**
         * 移动端传入门店实体，完成修改
         *
         */
        return storeInfoService.updateStore(storeInfo);
    }

}
